package com.example.za.account.dao;

import android.content.Context;

import com.example.za.account.R;
import com.example.za.account.model.ExpenseCat;
import com.example.za.account.model.User;
import com.example.za.account.utils.DBOpenHelper;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpenseCatDao {

    private Dao<ExpenseCat, Integer> mDao;

    public ExpenseCatDao(Context context) {
        mDao = DBOpenHelper.getInstance(context).getDao(ExpenseCat.class);
    }

    //根据编号查询类别
    public List<ExpenseCat> getExpenseCat(int userId) {
        List<ExpenseCat> cats = null;
        try {
            cats = mDao.queryForEq("ASuserId", userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cats;
    }

    //根据编号查询类别
    public List<ExpenseCat> getExpenseCat(User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("ASuserId", user);
        List<ExpenseCat> cats = null;
        try {
            cats = mDao.queryForFieldValues(map);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cats;
    }

    //添加类别
    public boolean addCategory(ExpenseCat expenseCat) {
        int row = 0;
        try {
            row = mDao.create(expenseCat);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row > 0;
    }

    //修改类别
    public boolean update(ExpenseCat expenseCat) {
        int row = 0;
        try {
            row = mDao.update(expenseCat);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row > 0;
    }

    //删除类别
    public boolean delete(ExpenseCat expenseCat) {
        int row = 0;
        try {
            row = mDao.delete(expenseCat);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row > 0;
    }

    public void initExpensesCat(User user) {
        int resId[] = {R.drawable.icon_shouru_type_qita, R.drawable.icon_zhichu_type_canyin,
                R.drawable.icon_zhichu_type_jiaotong, R.drawable.icon_zhichu_type_yanjiuyinliao,
                R.drawable.icon_zhichu_type_shuiguolingshi, R.drawable.icon_add_9, R.drawable.lingshi, R.drawable.maicai, R.drawable.yifu,
                R.drawable.richangyongpin, R.drawable.icon_zhichu_type_shoujitongxun, R.drawable.huazhuangpin,
                R.drawable.fangzu, R.drawable.dianying, R.drawable.icon_zhichu_type_taobao, R.drawable.huankuan,
                R.drawable.icon_shouru_type_hongbao, R.drawable.yaopinfei};
        String labels[] = {"一般", "餐饮", "交通", "酒水饮料", "水果", "投资", "零食", "买菜", "衣服鞋包", "生活用品",
                "话费", "护肤彩妆", "房租", "电影", "淘宝", "还钱", "红包", "药品"};
        List<ExpenseCat> cats = new ArrayList<>(resId.length);
        ExpenseCat expenseCat;
        for (int i = 0; i < resId.length; i++) {
            expenseCat = new ExpenseCat();
            expenseCat.setmUser(user);
            expenseCat.setmImageId(resId[i]);
            expenseCat.setmName(labels[i]);
            cats.add(expenseCat);
        }
        try {
            for (int i = 0, j = cats.size(); i < j; i++) {
                mDao.create(cats.get(i));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
